<template lang="">
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>数据统计</el-breadcrumb-item>
      <el-breadcrumb-item>年龄统计</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div id="myChart" :style="{width: '300px', height: '300px'}">

      </div>
    </el-card>

  </div>
</template>
<script>
    export default {
        data() {
            return {
                data1: [],
                data2: []
            }
        },
        mounted() {
            this.getage()


        },
        methods: {
            getage() {

                this.$axios.get('api/age').then(res => {

                    if (res.data.status == 200) {
                        for (const iterator of res.data.data) {
                            this.data1.push(iterator.age)
                            this.data2.push(iterator["count(s.age)"])
                            this.drawLine();
                        }
                    } else {
                        console.log('获取失败');
                    }
                })
            },
            drawLine() {
                // 基于准备好的dom，初始化echarts实例
                let myChart = this.$echarts.init(document.getElementById('myChart'))
                    // 绘制图表
                myChart.setOption({
                    title: {
                        text: '年龄报表'
                    },
                    tooltip: {
                        trigger: 'item'
                    },

                    xAxis: {
                        data: this.data1

                    },
                    yAxis: {},
                    series: [{
                        name: '年龄',
                        type: 'bar',
                        data: this.data2,
                        itemStyle: {
                            normal: {
                                color: function(params) {
                                    var colorList = ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc', ];
                                    var index = params.dataIndex % colorList.length;
                                    return colorList[index]
                                }
                            }
                        },

                    }]
                });
            }
        }
    }
</script>
<style lang="">

</style>